//
//  LGPatientRiskController.m
//  XieHeHeartDisease
//
//  Created by 1234 on 2024/1/26.
//

#import "LGPatientRiskController.h"
#import "LGPatientRiskCell.h"
@interface LGPatientRiskController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *saveButton;
/**   */
@property (nonatomic , strong)NSMutableDictionary *resultDic;
/**   */
@property (nonatomic , strong)NSArray *testArray;

@end

@implementation LGPatientRiskController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"危险分层";
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.saveButton];
    [self requestData];
    self.testArray = @[
        @{
            @"title":@"1.运动或恢复期症状及心电图改变",
            @"key":@"a1",
            @"items":@[@"A.运动或恢复期无心绞痛症状或心电图缺血改变",@"B.中度运动（5.0~6.9METs）或恢复期出现心绞痛症状或心电图缺血改变",@"C.低水平运动（<5.0METs）或恢复期出现心绞痛症状或心电图缺血改变"]
        },@{
            @"title":@"2.心律失常",
            @"key":@"a2",
            @"items":@[@"A.无休息或运动引起的复杂心律失常",@"B.休息或运动时未出现复杂室性心律失常",@"C.休息或运动时出现复杂室性心律失常"]
        },@{
            @"title":@"3.血运重建后并发症",
            @"key":@"a3",
            @"items":@[@"A.AMI溶栓血管再通PCI或CABG后血管再通且无并发症",@"B.AMI、PCI或CABG后无合并心源性休克或心力衰竭",@"C.AMI、PCI或CABG后合并心源性休克或心力衰竭"]
        },@{
            @"title":@"4.心理障碍",
            @"key":@"a4",
            @"items":@[@"A.无心理障碍（抑郁、焦虑等）",@"B.无严重心理障碍（抑郁、焦虑等）",@"C.严重心理障碍"]
        },@{
            @"title":@"5.左心室射血分数",
            @"key":@"a5",
            @"items":@[@"A.>50%",@"B.40%~49%",@"C.<40%"]
        },@{
            @"title":@"6.功能储备(METs)",
            @"key":@"a6",
            @"items":@[@"A.≥7.0",@"B.5.0~7.0",@"C.≤5.0"]
        },@{
            @"title":@"7.血肌钙蛋白浓度",
            @"key":@"a7",
            @"items":@[@"A.正常",@"B.升高"]
        }
    ];
//    self.resultDic = [NSMutableDictionary dictionaryWithDictionary:@{@"a1":@"",@"a2":@"",@"a3":@"",@"a4":@"",@"a5":@"",@"a6":@"",@"a7":@""}];
}

-(void)requestData{
    [RequestUtil withPOST:@"doctor/patient.profile/sdlEdit" parameters:@{@"patient_id":self.patientid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.resultDic = [NSMutableDictionary dictionaryWithDictionary:returnData[@"body"]];
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

//保存数据
-(void)saveDataAction{
    NSArray *keyArray = self.resultDic.allKeys;
    for (NSString *key in keyArray) {
        if(LGNSString(self.resultDic[key]).length == 0){
            NSString *index = [key substringFromIndex:1];
            [TooltipView showMessage:[NSString stringWithFormat:@"请完成第%@题",index] offset:0];
            return;
        }
    }
    [RequestUtil withPOST:@"/doctor/patient.profile/sdlSave" parameters:@{@"patient_id":self.patientid,@"body_json":[ObjectTool objectToJson:self.resultDic]} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            [TooltipView showMessage:@"保存成功" offset:0];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:@"保存失败，请重试" offset:0];
    }];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.testArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *itemArray = self.testArray[section][@"items"];
    return itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGPatientRiskCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSString *content = self.testArray[indexPath.section][@"items"][indexPath.row];
    NSString *key = LGNSString(self.testArray[indexPath.section][@"key"]);
    cell.title = content;
    content = [content substringToIndex:1];
    cell.isSelect = ([self.resultDic.allKeys containsObject:key] && [self.resultDic[key] isEqualToString:content])?YES:NO;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *key = LGNSString(self.testArray[indexPath.section][@"key"]);
    NSString *content = self.testArray[indexPath.section][@"items"][indexPath.row];
    content = [content substringToIndex:1];
    [self.resultDic setValue:content forKey:key];
    [self.tableView reloadData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(42);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(42))];
    UILabel *titleLabel = [UILabel lableWithText:LGNSString(self.testArray[section][@"title"]) colorString:@"#282828" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    titleLabel.frame = CGRectMake(viewPix(18), viewPix(15), Screen_W-viewPix(25), viewPix(23));
    [headView addSubview:titleLabel];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
}


-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(60)-bottomSafeBarHeight/3.0) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGPatientRiskCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(UIButton *)saveButton{
    if(!_saveButton){
        _saveButton = [UIButton buttonWithTitle:@"保存" topMargin:kMaxY(self.tableView.frame)+viewPix(5)];
        [_saveButton addTarget:self action:@selector(saveDataAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveButton;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
